#include "sdkconfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_err.h"
#include "esp_log.h"
#include "bsp_rmt.h"
#include "drv_pincontrol.h"
#include "drv_ws2812.h"

static const char *TAG = "main.c";
TimerHandle_t timer_5ms = NULL;
TimerHandle_t timer_10ms = NULL;
TimerHandle_t timer_20ms = NULL;

void TimerCallback(TimerHandle_t xTimer) {
    uint32_t timer_id = (uint32_t)pvTimerGetTimerID(xTimer);
    
    switch(timer_id) {
        case 0:
            
            break;
        case 1:
            
            break;
        case 2:
            
            break;
    }
}

static void System_Task_Handler(void* arg)
{
    for (;;)
    {     
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

void app_main(void)
{
    ESP_LOGI(TAG, "MCU BSP initial start up");
    Drv_IO_Control(NULL,E_PIN_INIT,0);  // init gpio
    Bsp_Rmt_Init();
    vTaskDelay(pdMS_TO_TICKS(100));
    ESP_LOGI(TAG, "MCU BSP initial ok");

    ESP_LOGI(TAG, "MCU SoftTimer creation start");
    timer_5ms = xTimerCreate("Timer5ms",pdMS_TO_TICKS(5), pdTRUE,(void*)0,TimerCallback);
    if (timer_5ms && xTimerStart(timer_5ms, 0) != pdPASS) {
        printf("5ms softer timer create fail!\n");
    }
    timer_10ms = xTimerCreate("Timer10ms",pdMS_TO_TICKS(10), pdTRUE,(void*)1,TimerCallback);
    if (timer_10ms && xTimerStart(timer_10ms, 0) != pdPASS) {
        printf("10ms softer timer create fail!\n");
    }
    timer_20ms = xTimerCreate("Timer20ms",pdMS_TO_TICKS(20), pdTRUE,(void*)2,TimerCallback);
    if (timer_20ms && xTimerStart(timer_20ms, 0) != pdPASS) {
        printf("20ms softer timer create fail!\n");
    }
    ESP_LOGI(TAG, "MCU SoftTimer creation ok");

    ESP_LOGI(TAG, "MCU task creation start");
    xTaskCreatePinnedToCore(System_Task_Handler,         "System_Task",           4096, NULL, 1, NULL,0);
    ESP_LOGI(TAG, "MCU task creation ok");


    while(1)
    {
        Dev_WS2812.color.B = 0;        
        Drv_WS2812_Control(&Dev_WS2812,E_WS2812_SET,0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        Dev_WS2812.color.B = 0;
        Drv_WS2812_Control(&Dev_WS2812,E_WS2812_SET,0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
